local exit_frame = 10

AddStategraphState('wilson',State{
    name = "webbert_give",
    tags = {'giving'},
    onenter = function(inst)
        inst.components.locomotor:Stop()
        inst.AnimState:PlayAnimation("give")
    end,
    timeline = {
        TimeEvent(8*FRAMES, function(inst)
            -- inst.SoundEmitter:PlaySound("dontstarve/common/together/houndwhistle")
            inst:PerformBufferedAction()
        end),
        TimeEvent(exit_frame*FRAMES, function(inst)
            -- inst.SoundEmitter:PlaySound("dontstarve/common/together/houndwhistle")
            inst.sg:RemoveStateTag("give")
            inst.sg:GoToState("idle")
        end),
    },
    -- ontimeout = function(inst)
    --     inst.sg:GoToState("idle", true)
    -- end,
    events =
    {
        EventHandler("animover", function(inst)
            inst.sg:GoToState("idle")
        end),
    },
})

AddStategraphState('wilson_client',State{
    name = 'webbert_give',
    tags = {'giving'},
    server_states = { "give" },
    onenter = function(inst)
        inst.components.locomotor:Stop()
        if not inst.sg:ServerStateMatches() then
            inst.AnimState:PlayAnimation("give")
        end
        inst:PerformPreviewBufferedAction()
        inst.sg:SetTimeout((exit_frame+4)*FRAMES)
    end,
    onupdate = function(inst)
        if inst.sg:ServerStateMatches() then
            if inst.entity:FlattenMovementPrediction() then
                inst.sg:GoToState("idle", "noanim")
            end
        elseif inst.bufferedaction == nil then
            inst.sg:GoToState("idle", true)
        end
    end,
    ontimeout = function(inst)
        inst:ClearBufferedAction()
        inst.sg:GoToState("idle", true)
    end,
})
